<script setup lang="ts">
import {useEquipmentStore} from '@/stores/equipmentStore.ts';
import {onMounted, ref} from "vue";
import router from "@/router/index.js";
import {useEquipmentTypeStore} from "@/stores/equipmentTypeStore.ts";
import type {ComponentSize} from "element-plus";



const equipmentStore = useEquipmentStore();
const equipmentTypeStore=useEquipmentTypeStore();



// 定义搜索标志
const getAllListFlag=ref(true);
const getByTypeListFlag=ref(false);
const getByNameListFlag=ref(false);




// 获取所有的设备，形成列表，同时获取所有数据函数，用于分页
onMounted( async ()=>{
  // 获取全部设备列表
  await equipmentStore.getEquipmentByPage(current.value,pageSize.value);
  await equipmentTypeStore.getFullEquipmentType(1, 10000);
  await equipmentStore.getEquipmentCount();

})

// 分页组件的每页条数值改变
const handleSizeChange=async (val:any)=>{
  if (getAllListFlag.value===true){
    await equipmentStore.getEquipmentByPage(current.value,val);
  }
  else if (getByTypeListFlag.value===true){
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    await equipmentStore.getEquipmentByType(typeFullName.value,1,current.value,val);
  }
  else if (getByNameListFlag.value===true){
    await   equipmentStore.getByMultifunction(multifunctionName.value,current.value,val);
  }
}
// 分页组件的第几页数值改变
const handleCurrentChange=async (val:any)=>{
  if (getAllListFlag.value===true){
    await equipmentStore.getEquipmentByPage(val,pageSize.value);
  }
  else if (getByTypeListFlag.value===true){
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    await equipmentStore.getEquipmentByType(typeFullName.value,1,val,pageSize.value);
    // typeValueList.value=[];
  }
  else if (getByNameListFlag.value===true){
    await equipmentStore.getByMultifunction(multifunctionName.value,val,pageSize.value);
  }
}

// 根据全设备类型名称查询
const typeValueList=ref([])
const typeFullName=ref('')


const getEquipmentByTypeName=async ()=>{
  getAllListFlag.value=false;
  getByTypeListFlag.value=true;
  getByNameListFlag.value=false;


  if (typeValueList.value===undefined){
    // 获取全部设备列表
    await equipmentStore.getEquipmentByPage(current.value,pageSize.value);

    // 获取全部设备类型
    await  equipmentStore.getEquipmentCount();
  }
  else {
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    // 这个函数，在store里面已经包含了一个获取counts的方法
    // equipmentCount.value=(await useGetCountByTypeNameAPI(typeName)).data;
    await  equipmentStore.getEquipmentByType(typeFullName.value,1,current.value,pageSize.value);

  }
}

// 多功能查询字符串
const multifunctionName=ref('')
const getListByMultifunctionName=async ()=>{
  getAllListFlag.value=false;
  getByTypeListFlag.value=false;
  getByNameListFlag.value=true;

  // 这个函数，在store里面已经包含了一个获取counts的方法
  //  equipmentCount.value=(await useGetCountByMultifunctionNameAPI(name)).data;

  await equipmentStore.getByMultifunction(multifunctionName.value,current.value,pageSize.value);
}


// 分页组件
const size = ref<ComponentSize>('small');
const background = ref(true)
const disabled = ref(false)
const current=ref(1);
const pageSize=ref(15);

// const getListByEncode=async ()=>{
//   // 直接使用和使用设备名的input框的数据来搜索
//   await equipmentStore.getOneByEncode(equipmentName.value);
//
// }

const reloadAllEquipmentList=async ()=>{
  // 获取全部设备列表
  await equipmentStore.getEquipmentByPage(current.value,pageSize.value);
  // 获取全部设备列表的总数量
  // await equipmentStore.getEquipmentCount();
  // 获取全部设备类型
  await equipmentTypeStore.getFullEquipmentType(1, 10000);
}


</script>

<template>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{"设备列表"}}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div>
    <div class="equipmentController">
      <div class="m-4">
        <div class="m-4">
          <el-cascader
              placeholder="请选择设备类型"
              :options="equipmentTypeStore.fullEquipmentType"
              filterable
              v-model="typeValueList"
              @change="getEquipmentByTypeName"
              clearable
              style="margin-right: 20px"
          />
          <el-input v-model="multifunctionName" clearable style="width: 350px" placeholder="请输入设备名称/品牌/创建人/修改人/编码/类型" @clear="reloadAllEquipmentList"  />&nbsp;
          <el-button type="info" size="small" @click="getListByMultifunctionName">名称查询</el-button>
          <el-button type="danger" size="small" @click="router.push({'path':'/home/equipment/equipmentTransfer'})">设备出场表单列表</el-button>
          <el-button type="danger" size="small" @click="router.push({'path':'/home/equipment/saveOneEquipment'})">添加新设备</el-button>
        </div>
      </div>
    </div>
  </div>

  <!--  分页显示设备数据-->
  <div class="demo-pagination-block" style="float: right">
    <el-pagination
        v-model:current-page="current"
        v-model:page-size="pageSize"
        :page-sizes="[10, 15, 30, 45]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="equipmentStore.equipmentCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>

  <!-- 对表格中的字段排序，在表格属性中添加:default-sort,后面参数是需要排序的字段，
       order:后面是排序要求,这里是descending,然后在需要排序的列元素中添加sortable属性-->
  <div >
    <el-table
        :data="equipmentStore.allEquipmentByPage"
        style="width: 100%"
        stripe
        border
        max-height="800"
        :default-sort="{prop:'updateTime',order:'descending',}"
    >
      <el-table-column prop="eqName" fixed label="设备名称" width="220" >
      </el-table-column>
      <el-table-column prop="brand" label="品牌" width="100" >
      </el-table-column>
      <el-table-column prop="model" label="型号" width="110" >
      </el-table-column>
      <el-table-column prop="pieces" label="单位" width="60" >
      </el-table-column>
      <el-table-column prop="number" label="数量" width="60">
      </el-table-column>
      <el-table-column prop="typeName" label="设备类型" width="180">
      </el-table-column>
<!--      <el-table-column prop="encode"  label="设备编号" width="200"    sortable >-->
<!--      </el-table-column>-->
      <el-table-column prop="location" label="设备所在地" width="230">
      </el-table-column>
      <el-table-column prop="description" label="备注" width="220">
      </el-table-column>
      <el-table-column  label="操作" width="220" fixed="right">
        <!--  在操作这一栏，添加一个template元素，加一个#default属性，通过这个属性，获取到这一行的数据，是个对象
                     scope：就是el-table表格中被选中的那一行的数据，通过scope获取到该行的数据-->
        <template #default="scope">
          <el-button size="small" type="warning" plain  @click="router.push({path:`/home/equipment/equipmentDetail/${scope.row.id}`});">详情</el-button>
          <el-button size="small" type="warning" plain  @click="router.push({path:`/home/equipment/saveOneEquipmentTransfer/${scope.row.id}`})">转场</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>



</template>

<style scoped lang="scss">
.block {
  margin-right: 260px;
}

.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }

  .sub-list {
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;

      li {
        width: 168px;
        height: 160px;


        a {
          text-align: center;
          display: block;
          font-size: 16px;

          img {
            width: 100px;
            height: 100px;
          }

          p {
            line-height: 40px;
          }

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;

    .head {
      // .xtx-more {
      //   position: absolute;
      //   top: 20px;
      //   right: 20px;
      // }

      // .tag {
      //   text-align: center;
      //   color: #999;
      //   font-size: 20px;
      //   position: relative;
      //   top: -20px;
      // }
    }

    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }


}

.home-banner {
  width: 1240px;
  height: 500px;
  margin: 0 auto;

  /* position: absolute;
  left: 0;
  top: 0;
  z-index: 98;
  */

  img {
    width: 100%;
    height: 500px;
  }
}

.table_contain {
  margin-left: 25px;
  padding-left: 35px;
}

.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.equipmentController{
  padding-left: 80px;
}

.equipmentParentType{
  appearance: none;
  text-align: center;
  height: 30px;
  width: 150px;
  //border-radius: 15px;
  padding: 0 8px;
  outline: 0;
  letter-spacing: 1px;
  color: #1a1616;
  font-weight: 600;
  background: rgba(45,45,45,.10);
  border: 1px solid rgba(255,255,255,.15);
  box-shadow: 0 2px 3px 0 rgba(0,0,0,.1) inset;
  text-shadow: 0 1px 2px rgba(0,0,0,.1);

}



</style>